package com.jqsoft.launcher.util;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringTools extends StringUtils {
	/**
	 * 正则,匹配a-z A-Z(英文大写或小写)
	 */
	private static final String REGEX_LowerOrUpperCase = "[A-Za-z]+";
	/**
	 * 正则,匹配0-9(数字)
	 */
	private static final String REGEX_NUM_ONLY = "[0-9]+";
	public static final char[] CHAR_RANDOMS = {
	    '1',
	    '2',
	    '3',
	    '4',
	    '5',
	    '6',
	    '7',
	    '8',
	    '9',
	    'A',
	    'B',
	    'C',
	    'D',
	    'E',
	    'F',
	    'G',
	    'H',
	    'I',
	    'J',
	    'K',
	    'L',
	    'M',
	    'N',
	    'O',
	    'P',
	    'Q',
	    'R',
	    'S',
	    'T',
	    'U',
	    'V',
	    'W',
	    'X',
	    'Y',
	    'Z',
	    'a',
	    'b',
	    'c',
	    'd',
	    'e',
	    'f',
	    'g',
	    'h',
	    'i',
	    'j',
	    'k',
	    'l',
	    'm',
	    'n',
	    'o',
	    'p',
	    'q',
	    'r',
	    's',
	    't',
	    'u',
	    'v',
	    'w',
	    'x',
	    'y',
	    'z'                                };

	private StringTools() {
	}

	/**
	 * 判断字符串是否为空，即为null或""
	 */
	public static boolean isEmpty(String str) {
		return ((str == null) || (str.length() == 0));
	}

	/**
	 * 判断字符串是否不为空，即不为null且不为""
	 */
	public static boolean isNotEmpty(String str) {
		return (!(isEmpty(str)));
	}

	/**
	 * 判断字符串是否为空白，即为null或为" "
	 */
	public static boolean isBlank(String str) {
		return ((str == null) || (str.trim().length() == 0));
	}

	/**
	 * 判断字符串是否不为空白，即不为null且不为" "
	 */
	public static boolean isNotBlank(String str) {
		return (!(isBlank(str)));
	}

	/**
	 * 判断传入的字符串，是否符合正则表达式
	 *
	 * @param str
	 *        字符串
	 * @param pattern
	 *        正则表达式
	 * @return
	 */
	public static boolean matches(String str, String pattern) {
		return Pattern.matches(pattern, str);
	}

	/**
	 * 生成随机字符串. 随机字符串的内容包含[1-9 A-Z a-z]的字符.
	 *
	 * @param length
	 *        必须为正整数 随机字符串的长度
	 * @return 随机字符串.
	 */
	public static String buildRandomStringByLength(int length) {
		if (length <= 0) {
			throw new IllegalArgumentException("length的取值范围是(0,+无穷)");
		}
		Random random = new Random(System.currentTimeMillis());
		StringBuffer ret = new StringBuffer();
		for (int i = 0; i < length; i++) {
			ret.append(CHAR_RANDOMS[random.nextInt(CHAR_RANDOMS.length)]);
		}
		random = null;
		return ret.toString();
	}
	
	/**随机生成单位长度的数字  1-9
	 * @param length
	 * @return 
	 */
	public static String buildRandomNumberByLength(int length){
		StringBuffer sb=new StringBuffer();
		for(int i=0;i<length;i++){
			String a = String.valueOf((int)(Math.random() * 9)+1) ;
			sb.append(a);
		}
		return sb.toString();
	}

	/**
	 * MD5加密
	 */
	public static String md5Coding(String str) {
		return DigestUtils.md5Hex(str);
	}
	
	/**
	 * 将null转化空""
	 * @param str
	 * @return str
	 */
	public static String changeNullToKong(String str) {
		if(str == null) return str = "";
		return str;
	}
	 public static byte[] shortArray2ByteArray(short[] data) {
			ByteBuffer bb = ByteBuffer.allocate(data.length*2);
			bb.order(ByteOrder.BIG_ENDIAN);
			
			for (short d : data) {
				bb.putShort(d);
			}
			
			return bb.array();
		}
		/**
		 * 正则匹配
		 * 
		 * @param string
		 *        字符串
		 * @param regex
		 *        正则表达式
		 * @return 验证结果
		 * @author www.TheWk.cn.vc
		 */
		private static boolean matcher(String string, String regex) {
			if (string == null) {
				return false;
			}
			Pattern p = Pattern.compile(regex);
			Matcher m = p.matcher(string);
			return m.matches();
		}

	 /**
		 * 验证是否只为数字(0~9) 是:返回true 否:返回false
		 * 
		 * @param string
		 *        被验证的字符串
		 * @author www.TheWk.cn.vc
		 */
		public static boolean isNumOnly(String string) {
			return matcher(string, REGEX_NUM_ONLY);
		}
		/**
		 * 验证是否只为英文(A-Z a-z) 是:返回true 否:返回false
		 * 
		 * @param string
		 *        被验证的字符串
		 * @author www.TheWk.cn.vc
		 */
		public static boolean isLowerOrUpperCase(String string) {
			return matcher(string, REGEX_LowerOrUpperCase);
		}

}